package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.utils.IDUtils;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.DepositPriceSetDtoMapper;
import com.starlink.model.domain.DepositPriceSetDO;
import com.starlink.mapper.DepositPriceSetMapper;
import com.starlink.model.request.DepositPriceSetRequest;
import com.starlink.service.DepositPriceSetService;
import com.starlink.model.util.DepositPriceSetConvert;
import com.starlink.model.vo.DepositPriceSetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class DepositPriceSetServiceImpl extends ServiceImpl<DepositPriceSetMapper, DepositPriceSetDO> implements DepositPriceSetService {
    @Autowired
    private DepositPriceSetMapper depositPriceSetMapper;

    @Override
    public DepositPriceSetVO getByKey(long id) {
        DepositPriceSetVO depositPriceSetVO = DepositPriceSetDtoMapper.INSTANCE.do2VO(getById(id));
        return depositPriceSetVO;
    }

    @Override
    public PageInfo<DepositPriceSetVO> pageQuery(Pageable pageable,DepositPriceSetRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<DepositPriceSetDO> list = depositPriceSetMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<DepositPriceSetVO> depositPriceSetVOList = DepositPriceSetConvert.buildDepositPriceSetVoList(list);
        return new PageInfo(depositPriceSetVOList);
    }

    @Override
    public List<DepositPriceSetVO> listQuery(DepositPriceSetRequest request) {
        List<DepositPriceSetDO> list = depositPriceSetMapper.selectDOList(DepositPriceSetDtoMapper.INSTANCE.request2DO(request));
        List<DepositPriceSetVO> depositPriceSetVOS = DepositPriceSetConvert.buildDepositPriceSetVoList(list);
        return depositPriceSetVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(DepositPriceSetRequest request) {
        request.setId(IDUtils.getId());
        save(DepositPriceSetDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(DepositPriceSetRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(DepositPriceSetDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        depositPriceSetMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public DepositPriceSetVO selectOne() {
        DepositPriceSetDO priceSetDO = this.lambdaQuery().last("limit 1").one();
        return DepositPriceSetDtoMapper.INSTANCE.do2VO(priceSetDO);
    }
}
